package com.dlc.garbagerecyclebox.bean;


import android.util.ArrayMap;

import com.dlc.commonbiz.base.exception.BaseSerialPortException;
import com.dlc.commonbiz.base.util.GsonUtil;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

/**
 * @author :      fangbingran
 * @aescription : 回收结果
 * @date :        2019/08/01  20:02
 */
public class RecyclingResultBean implements Cloneable {

    private ArrayMap<String, OpenDoorResultBean> list;
    private List<Integer> listAddress;

    public RecyclingResultBean(List<Integer> listAddress) {
        this.listAddress = listAddress;
        list = new ArrayMap<>();
    }

    public ArrayMap<String, OpenDoorResultBean> getList() {
        return list;
    }


    public void handleException(BaseSerialPortException e) {
        if (list.size() == 0) {
            OpenDoorResultBean openDoorResultBean = new OpenDoorResultBean();
            //等待关门,说明门还没关闭
            openDoorResultBean.setFault(true);
            openDoorResultBean.setFaultMsg(e.getMessage());
            openDoorResultBean.setRecyclingInfoBean(null);
            openDoorResultBean.setAddress(e.getAddress());
            openDoorResultBean.setOpenDoor(false);
            addOpenDoorResultBean(e.getAddress(), openDoorResultBean);
            return;
        }
        Iterator<HashMap.Entry<String, OpenDoorResultBean>> entries = list.entrySet().iterator();
        while (entries.hasNext()) {
            HashMap.Entry<String, OpenDoorResultBean> entry = entries.next();
            OpenDoorResultBean openDoorResultBean = entry.getValue();
            if (openDoorResultBean.getAddress() == e.getAddress()) {
                openDoorResultBean.setFault(true);
                openDoorResultBean.setFaultMsg(e.getMessage());
                openDoorResultBean.setRecyclingInfoBean(null);
                openDoorResultBean.setAddress(e.getAddress());
                openDoorResultBean.setOpenDoor(false);
                return;
            }
        }
        OpenDoorResultBean openDoorResultBean = new OpenDoorResultBean();
        //等待关门,说明门还没关闭
        openDoorResultBean.setFault(true);
        openDoorResultBean.setFaultMsg(e.getMessage());
        openDoorResultBean.setRecyclingInfoBean(null);
        openDoorResultBean.setAddress(e.getAddress());
        openDoorResultBean.setOpenDoor(false);
        addOpenDoorResultBean(e.getAddress(), openDoorResultBean);
    }

    public void addOpenDoorResultBean(int address, OpenDoorResultBean openDoorResultBean) {
        list.put(address + "_recycling", openDoorResultBean);
    }

    public void handleSuccess(RecyclingInfoBean recyclingInfoBean) {
        if (list.size() == 0) {
            OpenDoorResultBean openDoorResultBean = new OpenDoorResultBean();
            //等待关门,说明门还没关闭
            openDoorResultBean.setRecyclingInfoBean(recyclingInfoBean.clone());
            openDoorResultBean.setAddress(recyclingInfoBean.getDeviceAddress());
            if (recyclingInfoBean.isWaitingELockClose()) {
                //等待关门,说明门还没关闭
                openDoorResultBean.setOpenDoor(true);
            }
            if (recyclingInfoBean.getFaultCodeBean().isHasFault(com.dlc.garbagerecyclebox.FaultCode.LOWER_STOREHOUSE_DOOR_OPEN_DOOR_OVER_TIME)
                    || recyclingInfoBean.getFaultCodeBean().isHasFault(com.dlc.garbagerecyclebox.FaultCode.LOWER_STOREHOUSE_DOOR_OPEN_DOOR)
                    || recyclingInfoBean.getFaultCodeBean().isHasFault(com.dlc.garbagerecyclebox.FaultCode.COMMUNICATION)
                    || (recyclingInfoBean.isDeliveryCompleted() && !openDoorResultBean.isOpenDoor())) {
                openDoorResultBean.setFault(true);
                openDoorResultBean.setFaultMsg(openDoorResultBean.getFaultMsg());
            }
            addOpenDoorResultBean(recyclingInfoBean.getDeviceAddress(), openDoorResultBean);
            return;
        }
        Iterator<HashMap.Entry<String, OpenDoorResultBean>> entries = list.entrySet().iterator();
        while (entries.hasNext()) {
            HashMap.Entry<String, OpenDoorResultBean> entry = entries.next();
            OpenDoorResultBean openDoorResultBean = entry.getValue();
            if (openDoorResultBean.getAddress() == recyclingInfoBean.getDeviceAddress()) {
                if (recyclingInfoBean.isWaitingELockClose()) {
                    //等待关门,说明门还没关闭
                    openDoorResultBean.setOpenDoor(true);
                }
                if (recyclingInfoBean.getFaultCodeBean().isHasFault(com.dlc.garbagerecyclebox.FaultCode.LOWER_STOREHOUSE_DOOR_OPEN_DOOR_OVER_TIME)
                        || recyclingInfoBean.getFaultCodeBean().isHasFault(com.dlc.garbagerecyclebox.FaultCode.COMMUNICATION)
                        || recyclingInfoBean.getFaultCodeBean().isHasFault(com.dlc.garbagerecyclebox.FaultCode.LOWER_STOREHOUSE_DOOR_OPEN_DOOR)
                        || (recyclingInfoBean.isDeliveryCompleted() && !openDoorResultBean.isOpenDoor())) {
                    openDoorResultBean.setFault(true);
                    openDoorResultBean.setFaultMsg(openDoorResultBean.getFaultMsg());
                }
                openDoorResultBean.setRecyclingInfoBean(recyclingInfoBean.clone());
                return;
            }
        }
        OpenDoorResultBean openDoorResultBean = new OpenDoorResultBean();
        openDoorResultBean.setRecyclingInfoBean(recyclingInfoBean.clone());
        openDoorResultBean.setAddress(recyclingInfoBean.getDeviceAddress());
        //等待关门,说明门还没关闭
        if (recyclingInfoBean.isWaitingELockClose()) {
            //等待关门,说明门还没关闭
            openDoorResultBean.setOpenDoor(true);
        }
        if (recyclingInfoBean.getFaultCodeBean().isHasFault(com.dlc.garbagerecyclebox.FaultCode.LOWER_STOREHOUSE_DOOR_OPEN_DOOR_OVER_TIME)
                || recyclingInfoBean.getFaultCodeBean().isHasFault(com.dlc.garbagerecyclebox.FaultCode.LOWER_STOREHOUSE_DOOR_OPEN_DOOR)
                || recyclingInfoBean.getFaultCodeBean().isHasFault(com.dlc.garbagerecyclebox.FaultCode.COMMUNICATION)
                || (recyclingInfoBean.isDeliveryCompleted() && !openDoorResultBean.isOpenDoor())) {
            openDoorResultBean.setFault(true);
            openDoorResultBean.setFaultMsg(openDoorResultBean.getFaultMsg());
        }
        addOpenDoorResultBean(recyclingInfoBean.getDeviceAddress(), openDoorResultBean);
    }

    /**
     * 是否完成开门动作
     *
     * @return
     */
    public boolean isCompleteOpenDoorAction() {
        int count = 0;
        if (list != null && listAddress != null && list.size() == listAddress.size()) {
            Iterator<HashMap.Entry<String, OpenDoorResultBean>> entries = list.entrySet().iterator();
            while (entries.hasNext()) {
                HashMap.Entry<String, OpenDoorResultBean> entry = entries.next();
                OpenDoorResultBean openDoorResultBean = entry.getValue();
                if (openDoorResultBean != null && (openDoorResultBean.isFault() || openDoorResultBean.isOpenDoor())) {
                    count++;
                }

            }
        }
        return count == listAddress.size();
    }

    /**
     * 是否关闭所有的门
     *
     * @return
     */
    public boolean isCloseAllDoor() {
        if (!isCompleteOpenDoorAction()) {
            return false;
        }
        Iterator<HashMap.Entry<String, OpenDoorResultBean>> entries = list.entrySet().iterator();
        while (entries.hasNext()) {
            HashMap.Entry<String, OpenDoorResultBean> entry = entries.next();
            OpenDoorResultBean openDoorResultBean = entry.getValue();
            RecyclingInfoBean recyclingInfoBean = openDoorResultBean.getRecyclingInfoBean();
            if (recyclingInfoBean != null) {
                DeviceInfoBean.SensorStatusBean sensorStatusBean = recyclingInfoBean.getSensorStatusBean();
                //等待关门,说明门还没关闭
                if (recyclingInfoBean.isWaitingELockClose()) {
                    return false;
                }
                if (sensorStatusBean != null) {
                    //电磁锁上限位,电磁锁下限位没关好,所以门没关
                    if (sensorStatusBean.isELockUpperLimit() || sensorStatusBean.isELockUpperLowerLimit()) {
                        return false;
                    }
                }
            }
        }
        return true;
    }

    /**
     * 是否所有投递完成
     *
     * @return
     */
    public boolean isDeliveryAllCompleted() {
        if (!isCompleteOpenDoorAction()) {
            return false;
        }
        int count = 0;
        Iterator<HashMap.Entry<String, OpenDoorResultBean>> entries = list.entrySet().iterator();
        while (entries.hasNext()) {
            HashMap.Entry<String, OpenDoorResultBean> entry = entries.next();
            OpenDoorResultBean openDoorResultBean = entry.getValue();
            RecyclingInfoBean recyclingInfoBean = openDoorResultBean.getRecyclingInfoBean();
            if (openDoorResultBean.isFault() || (recyclingInfoBean != null && recyclingInfoBean.isDeliveryCompleted())) {
                count++;
            }
        }
        return count == list.size();
    }

    /**
     * 是否投递完成
     *
     * @return
     */
    public boolean isDeliveryCompleted(int address) {
        Iterator<HashMap.Entry<String, OpenDoorResultBean>> entries = list.entrySet().iterator();
        while (entries.hasNext()) {
            HashMap.Entry<String, OpenDoorResultBean> entry = entries.next();
            OpenDoorResultBean openDoorResultBean = entry.getValue();
            RecyclingInfoBean recyclingInfoBean = openDoorResultBean.getRecyclingInfoBean();
            if (address == openDoorResultBean.getAddress()) {
                return openDoorResultBean.isFault() || (recyclingInfoBean != null && recyclingInfoBean.isDeliveryCompleted());
            }

        }
        return false;
    }

    /**
     * 是否投递开门成功
     *
     * @return
     */
    public boolean isOpenDoorSuccess(int address) {
        Iterator<HashMap.Entry<String, OpenDoorResultBean>> entries = list.entrySet().iterator();
        while (entries.hasNext()) {
            HashMap.Entry<String, OpenDoorResultBean> entry = entries.next();
            OpenDoorResultBean openDoorResultBean = entry.getValue();
            if (address == openDoorResultBean.getAddress()) {
                return openDoorResultBean.isOpenDoor();
            }
        }
        return false;
    }

    /**
     * 是否成功打开所有门
     *
     * @return
     */
    public boolean isOpenAllDoorSuccess() {
        if (!isCompleteOpenDoorAction()) {
            return false;
        }
        Iterator<HashMap.Entry<String, OpenDoorResultBean>> entries = list.entrySet().iterator();
        boolean isOpen = true;
        while (entries.hasNext()) {
            HashMap.Entry<String, OpenDoorResultBean> entry = entries.next();
            OpenDoorResultBean openDoorResultBean = entry.getValue();
            if (openDoorResultBean != null) {
                if (!openDoorResultBean.isOpenDoor()) {
                    isOpen = false;
                    break;
                }
            }
        }
        return isOpen;
    }

    /**
     * 是否成功打其中一个门
     *
     * @return
     */
    public boolean isOpenDoorOneSuccess() {
        if (!isCompleteOpenDoorAction()) {
            return false;
        }
        Iterator<HashMap.Entry<String, OpenDoorResultBean>> entries = list.entrySet().iterator();
        boolean isOpen = false;
        while (entries.hasNext()) {
            HashMap.Entry<String, OpenDoorResultBean> entry = entries.next();
            OpenDoorResultBean openDoorResultBean = entry.getValue();
            if (openDoorResultBean != null) {
                if (openDoorResultBean.isOpenDoor()) {
                    isOpen = true;
                    break;
                }
            }
        }
        return isOpen;
    }

    public void setList(ArrayMap<String, OpenDoorResultBean> list) {
        this.list = list;
    }

    public List<Integer> getListAddress() {
        return listAddress;
    }

    public void setListAddress(List<Integer> listAddress) {
        this.listAddress = listAddress;
    }


    public static class OpenDoorResultBean implements Cloneable {
        /**
         * 箱子地址
         */
        private int address;
        /**
         * 是否打开成功
         */
        private boolean isOpenDoor;
        /**
         * 是否故障
         */
        private boolean isFault;
        /**
         * 错误解析
         */
        private String faultMsg = "正常";
        /**
         * 回收信息
         */
        private RecyclingInfoBean recyclingInfoBean;

        /**
         * 是否投递完成
         *
         * @return
         */
        public boolean isDeliveryCompleted() {
            return isFault || (recyclingInfoBean != null && recyclingInfoBean.isDeliveryCompleted());
        }

        public int getAddress() {
            return address;
        }

        public void setAddress(int address) {
            this.address = address;
        }

        public boolean isOpenDoor() {
            return isOpenDoor;
        }

        public void setOpenDoor(boolean openDoor) {
            isOpenDoor = openDoor;
        }

        public String getFaultMsg() {
            return faultMsg;
        }

        public void setFaultMsg(String faultMsg) {
            this.faultMsg = faultMsg;
        }

        public boolean isFault() {
            return isFault;
        }

        public void setFault(boolean fault) {
            isFault = fault;
        }

        public RecyclingInfoBean getRecyclingInfoBean() {
            return recyclingInfoBean;
        }

        public void setRecyclingInfoBean(RecyclingInfoBean recyclingInfoBean) {
            this.recyclingInfoBean = recyclingInfoBean;
        }

        @Override
        public OpenDoorResultBean clone() {
            OpenDoorResultBean openDoorResultBean = null;
            try {
                openDoorResultBean = (OpenDoorResultBean) super.clone();
                if (recyclingInfoBean != null) {
                    openDoorResultBean.setRecyclingInfoBean(recyclingInfoBean.clone());
                }
            } catch (CloneNotSupportedException e) {
                e.printStackTrace();
            }
            return openDoorResultBean;
        }

        @Override
        public String toString() {
            return "结果{" +
                    "地址=" + address +
                    ", 是否开门=" + isOpenDoor +
                    ", 是否失败=" + isFault +
                    ", 失败信息='" + faultMsg + '\'' +
                    ", 回收信息=" + recyclingInfoBean != null ? recyclingInfoBean.toString() : "" +
                    '}';
        }
    }

    private String listOpenDoorResultBeanToString() {
        StringBuffer stringBuffer = new StringBuffer();
        if (list != null && list.size() > 0) {
            Iterator<HashMap.Entry<String, OpenDoorResultBean>> entries = list.entrySet().iterator();
            while (entries.hasNext()) {
                HashMap.Entry<String, OpenDoorResultBean> entry = entries.next();
                OpenDoorResultBean openDoorResultBean = entry.getValue();
                if (openDoorResultBean != null) {
                    stringBuffer.append(",");
                    stringBuffer.append(openDoorResultBean.toString());
                }
            }
        }
        return stringBuffer.toString();
    }

    @Override
    public String toString() {
        return "回收结果{" + listOpenDoorResultBeanToString() +
                ", 命令数据=" + GsonUtil.getInstance().parseObjToJsonStr(listAddress) +
                '}';
    }
}
